/*
 * @Author: cqg
 * @Date: 2020-11-10 23:18:06
 * @LastEditors: cqg
 * @LastEditTime: 2020-11-22 23:11:48
 * @Description: file content
 */
import { getDict } from "~/utils";

import {
    searchWarehousingList,
    deleteRowById,
    exportWarehousing
} from "@/views/wy-asset-management/warehousing/api/warehousing.js";
import { mapGetters } from "vuex";

export default {
    name: "resource-manage",
    provide () {
        return {
            initList: this.query
        };
    },
    computed: {
        ...mapGetters(["permissions"])
    },
    data () {
        return {
            //按钮权限
            warehousingDel: false,
            warehousingEdit: false,
            loading: true,
            //表单数据
            tableData: [],
            // 勾选
            selectDataArr: [],
            menuList: [{ export: "导出" }],
            //过滤条件
            filterList: [
                {
                    label: "资产分类",
                    inputType: "dict",
                    dictName: "asset-classification",
                    name: "assetClassificationList"
                },
                {
                    label: "使用状况",
                    inputType: "dict",
                    dictName: "asset-use-status",
                    name: "useStatusList"
                }
            ],
            // 分页
            pager: {
                total: 0, // 总页数
                current: 1, // 当前页数
                size: 20 // 每页显示多少条
            },
            // 请求参数
            params: {
                current: 1,
                size: 20,
                keyword: ""
            }
        };
    },
    //载入事件
    mounted () {
        this.warehousingDel = this.permissions["warehousingDel"];
        this.warehousingEdit = this.permissions["warehousingEdit"];
        //查询
        this.query();
    },
    methods: {
        // 菜单点击监听
        handleMenuClick (type) {
            type == "export" && this.exportReg();
        },
        query (params = {}) {
            let req = { ...this.params, ...params };
            this.loading = true;
            !req.assetClassificationList && delete req.assetClassificationList;
            !req.useStatusList && delete req.useStatusList;
            searchWarehousingList(req)
                .then(res => {
                    const { current, size, total, records } = res.data.data;
                    this.tableData = records;
                    this.params = req;
                    this.pager = { total, current, size };
                    this.loading = false;
                })
                .catch(error => {
                    this.$message({
                        type: "error",
                        message: "查询失败"
                    });
                })
                .finally(() => (this.loading = false));
        },

        /**
         * 列表导出
         */
        exportReg (params = { size: 9999 }) {
            let req = { ...this.params, ...params };
            exportWarehousing(req).then(res => {
                // 关闭loading
                this.loading = false;
                const content = res.data;
                const blob = new Blob([content]); // 构造一个blob对象来处理数据
                const fileName = "已入库记录.xls"; // 导出文件名
                // 对于<a>标签，只有 Firefox 和 Chrome（内核） 支持 download 属性
                // IE10以上支持blob但是依然不支持download
                if ("download" in document.createElement("a")) {
                    // 支持a标签download的浏览器
                    const link = document.createElement("a"); // 创建a标签
                    link.download = fileName; // a标签添加属性
                    link.style.display = "none";
                    link.href = URL.createObjectURL(blob);
                    document.body.appendChild(link);
                    link.click(); // 执行下载
                    URL.revokeObjectURL(link.href); // 释放url
                    document.body.removeChild(link); // 释放标签
                } else {
                    // 其他浏览器
                    navigator.msSaveBlob(blob, fileName);
                }
                this.btnSendTem = false;
            });
        },

        /**
         * 删除
         * @param scope
         */
        deleteRow (scope) {
            //变更状态 审批状态 10待发起(初始状态) 11变更未通过  12待变更  13核实未通过  14待核实  15办结
            //处置状态  10 未处置 11 待审核 12审核不通过 13办结
            if (
                !(
                    (scope.row.changeStatus == "15" ||
                        scope.row.changeStatus == "10" ||
                        scope.row.changeStatus == "11") &&
                    (scope.row.disposalStatus == "10" ||
                        scope.row.disposalStatus == "12" ||
                        scope.row.disposalStatus == "13")
                )
            ) {
                this.$alert("只有未操作状态才能删除", "提示", {
                    confirmButtonText: "确定"
                });
                return;
            }

            this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            })
                .then(() => {
                    deleteRowById(scope.row.assetFixedWarehousingId)
                        .then(resp => {
                            this.$message({
                                type: "success",
                                message: "删除成功!"
                            });
                            this.query();
                        })
                        .catch(error => {
                            this.$message({
                                type: "error",
                                message: "操作失败"
                            });
                        })
                        .finally(() => (this.loading = false));
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消删除"
                    });
                });
        },
        // 码值转换
        dictVal_assetClassification (row, column, cellValue) {
            let obj = getDict("asset-classification").find(
                p => p.value == cellValue
            );
            if (obj) {
                return obj.label;
            }
            return "";
        },

        //    码值转换
        dictVal_useStatusList (row, column, cellValue) {
            let obj = getDict("asset-use-status").find(
                p => p.value == cellValue
            );
            if (obj) {
                return obj.label;
            }
            return "";
        }
    }
};
